package com.xiaoxie.web.ai;

import cn.dev33.satoken.stp.StpUtil;
import io.github.pigmesh.ai.deepseek.core.DeepSeekClient;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionModel;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionRequest;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai")
@Tag(name = "AI",description = "deepSeek相关")
public class DeepSeekController {

    @Resource
    private DeepSeekClient deepSeekClient;

    @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @Operation(summary = "聊天")
    public Flux<ChatCompletionResponse> chat(@RequestParam("prompt") String prompt) {
        return deepSeekClient.chatFluxCompletion(prompt);
    }

    @GetMapping(value = "/simple-chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @Operation(summary = "简单聊天")
    public Flux<ChatCompletionResponse> chatAdvanced(@RequestParam("prompt") String prompt) {
        String tokenValue = StpUtil.getTokenValue();
        System.out.println(tokenValue);
        ChatCompletionRequest request = ChatCompletionRequest.builder()
                // 模型选择，支持 DEEPSEEK_CHAT、DEEPSEEK_REASONER 等
                .model(ChatCompletionModel.DEEPSEEK_CHAT)
                // 添加用户消息
                .addUserMessage(prompt)
                // 设置最大生成 token 数，默认 2048
                .maxTokens(1000)
        .build();

        return deepSeekClient.chatFluxCompletion(request);
    }
}

